package com.common.www.service.global;

import org.springframework.boot.base.jpa.service.BaseService;
import com.common.www.bean.entity.global.GlobalConfig;
import com.common.www.data.global.GlobalConfigEnum;
import com.common.www.repository.global.GlobalConfigRepository;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


/**
 * Created by cwx183898 on 2017/12/14.
 */
@Service
@Transactional
public class GlobalConfigService extends BaseService<GlobalConfig, Long> {
    @Autowired
    private GlobalConfigRepository repository;
    @Value("${server.servlet.context-path}")
    private String projectName;
    @Value("${ipAddress}")
    private String ipAddress;

    @Override
    public GlobalConfigRepository getRepository() {
        return repository;
    }


    /**
     * 得到配置,如果该配置不存在的话就会以默认值创建该配置
     */
    public String getConfig(GlobalConfigEnum globalConfigEnum, String defaultValue) {
        if (globalConfigEnum == null || defaultValue == null) {//该方法必须提供默认值
            return null;
        }
        GlobalConfig find = new GlobalConfig();
        find.setName(globalConfigEnum.name());

        GlobalConfig config = findOne(Example.of(find));
        if (config == null) {//存储并返回默认配置
            GlobalConfig save = new GlobalConfig();
            save.setName(globalConfigEnum.name());
            save.setDescription(globalConfigEnum.getDescribe());
            save.setValue(defaultValue + "");
            save(save);
            return defaultValue;
        } else {//取配置里的值
            return config.getValue();
        }
    }

    /**
     * 设置配置
     */
    public String setConfig(GlobalConfigEnum globalConfigEnum, String value) {
        if (globalConfigEnum == null || value == null) {//该方法必须提供默认值
            return null;
        }
        GlobalConfig find = new GlobalConfig();
        find.setName(globalConfigEnum.name());

        GlobalConfig config = findOne(Example.of(find));
        if (config == null) {//存储并返回默认配置
            GlobalConfig save = new GlobalConfig();
            save.setName(globalConfigEnum.name());
            save.setDescription(globalConfigEnum.getDescribe());
            save.setValue(value + "");
            save(save);
            return value;
        } else {//取配置里的值
            config.setValue(value);
            return config.getValue();
        }
    }

    public String getConfig(GlobalConfigEnum globalConfigEnum) {
        return getConfig(globalConfigEnum, globalConfigEnum.getDefaultValue());
    }

    //===========================一些常规的字段封装=================

    /**
     * 是否是生产环境针对一些特殊功能可以用这个值来做判断
     */
    public boolean isRelease() {
        boolean flag = "true".equals(getConfig(GlobalConfigEnum.RELEASE));

        if (flag && (ipAddress.indexOf("192.168") > -1
//                || ipAddress.equals("localhost")
        )) {//包含此信息的时候默认切换到调试模式去
            setConfig(GlobalConfigEnum.RELEASE, "false");
        }
        return flag;
    }

    public String getProjectName() {
        return projectName;
    }
}
